//
//  AppDelegate.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-6.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "AppDelegate.h"

#import "ViewController.h"
#import "AuthorManager.h"
#import "SinaWeiboManager.h"
#import "MobClick.h"

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [_navController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"app lauch start");
    //发送友盟统计数据
    [MobClick startWithAppkey:@"515939e356240bb714037d6e"];
    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    [self.window makeKeyAndVisible];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.navController = [[[UINavigationController alloc] initWithRootViewController:self.viewController] autorelease];
    self.navController.navigationBarHidden = YES;
    self.navController.delegate = self;
    self.viewController.wantsFullScreenLayout = YES;
    self.navController.wantsFullScreenLayout = YES;
    
    self.window.rootViewController = self.navController;
    NSLog(@"app lauch end");
    
    //启动用户模块，自动登录
    [AuthorManager defaultManager];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"auto_login"] boolValue]) {
        NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
        NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
        [[AuthorManager defaultManager] loginWithUsername:username password:password];
    }
    //启动下载模块，并监测网络状况
    [DownloadManager defaultManager];
    //加载历史播放记录
    [PlayerHistoryManager defaultManager];
    
    return YES;
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    viewController.wantsFullScreenLayout = YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [[DownloadManager defaultManager] save];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [[DataRequest sharedRequest] getCategory];
    [[DataRequest sharedRequest] getAppUpdateInfo];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [[DownloadManager defaultManager] save];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [[[SinaWeiboManager defaultManager] sinaweibo] handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [[[SinaWeiboManager defaultManager] sinaweibo] handleOpenURL:url];
}

@end
